<?php

namespace App\Models;

use App\Http\Controllers\ApiController;
use Illuminate\Database\Eloquent\Model;

class RechargeCommission extends Model
{
    public $table = 'recharge_commission';
    public static function getRechargeCommissionList($input)
    {
        $data =  $data = self::join('user as u1','recharge_commission.from_uid','=','u1.id')
            ->join('user as u2','recharge_commission.to_uid','=','u2.id')
            ->join('vip','vip.uid','=','recharge_commission.from_uid')
            ->select('recharge_commission.id','u1.user_nickname as from_nick_name','u1.user_name as from_user_name',
                'u2.user_nickname as to_nick_name','u2.user_name as to_user_name','vip.vip_card_sn',
                'recharge_commission.order_sn','recharge_commission.commission_rate','recharge_commission.commission','recharge_commission.created_at',
                'recharge_commission.pay_status','recharge_commission.updated_at','recharge_commission.direction','recharge_commission.total_amount'
            )->orderBy('updated_at','desc');

        if (!empty($input['key'])&&!empty($input['val'])){
            $key = $input['key'];
            $val = $input['val'];
            if ($key=='from_user_name'){
                $data = $data->where('u1.user_name',$val);

            }else if ($key == 'to_user_name'){
                $data = $data->where('u2.user_name',$val);
            }else if ($key == 'vip_card_sn'){
                $data = $data->where('vip.vip_card_sn',$val);
            }else if ($key == 'total_amount'){
                $val = explode('-',$val);
                asort($val);
                if (empty($val[0])||empty($val[1])){
                    return [];
                }
                $data = $data->whereBetween('recharge_commission.total_amount',$val);
            }else if ($key = 'created_at' ){
                $val  = json_decode($val,true);
                asort($val);
                $data = $data->whereBetween('recharge_commission.created_at',$val);
            }else{
                return [];
            }
        }
        return $data ->get()->toArray();
    }


    public static function addRechargeCommissionRecord($data)
    {
        $data  = ApiController::createData('recharge_commission',$data);
        $flag  =  self::insert($data);
        return $flag;
    }
}
